package com.dream.inspect.modules.business.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.dream.inspect.common.utils.DateUtils;
import com.dream.inspect.common.utils.JsonUtil;
import com.dream.inspect.modules.app.controller.bean.SupervisionQuestionBean;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.R;
import com.dream.inspect.common.validator.ValidatorUtils;
import com.dream.inspect.modules.business.entity.SuperviseImageEntity;
import com.dream.inspect.modules.business.entity.SuperviseQuestionEntity;
import com.dream.inspect.modules.business.entity.SuperviseQuestionModifyEntity;
import com.dream.inspect.modules.business.entity.SuperviseRecordEntity;
import com.dream.inspect.modules.business.service.SuperviseImageService;
import com.dream.inspect.modules.business.service.SuperviseQuestionModifyService;
import com.dream.inspect.modules.business.service.SuperviseQuestionService;
import com.dream.inspect.modules.business.service.SuperviseRecordService;
import com.dream.inspect.modules.dream.pojo.SysUserFormMap;
import com.dream.inspect.modules.sys.controller.AbstractController;
import com.dream.inspect.modules.sys.entity.SysUserEntity;



/**
 * 督察问题整改记录表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-06-16 17:04:51
 */
@RestController
@CrossOrigin
@RequestMapping("business/supervisequestionmodify")
public class SuperviseQuestionModifyController extends AbstractController {
    @Autowired
    private SuperviseQuestionModifyService superviseQuestionModifyService;
    @Autowired
    private SuperviseImageService superviseImageService;
    @Autowired
    private SuperviseQuestionService superviseQuestionService;
    @Autowired
    SuperviseRecordService superviseRecordService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:supervisequestionmodify:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = superviseQuestionModifyService.queryPage(params);

        return R.ok().put("page", page);
    }
    
    /**
     * 列表
     */
    @RequestMapping("/listMultiple")
    @RequiresPermissions("business:listMultiple")
    public R listMultiple(@RequestParam Map<String, String> params){
    	SysUserFormMap user = getSessionUser();
    	params.put("deptId", String.valueOf(user.getLong("deptId")));
		params.put("v2", user.get("roleId").toString());
        PageUtils page = superviseQuestionModifyService.selectSuperviseQuestionModifyListPage(params);

        return R.ok().put("page", page).put("user", user);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:supervisequestionmodify:info")
    public R info(@PathVariable("id") Long id){
        SuperviseQuestionModifyEntity superviseQuestionModify = superviseQuestionModifyService.selectById(id);

        return R.ok().put("superviseQuestionModify", superviseQuestionModify);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:supervisequestionmodify:save")
    public R save(@RequestBody SuperviseQuestionModifyEntity superviseQuestionModify){
        superviseQuestionModifyService.insert(superviseQuestionModify);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:supervisequestionmodify:update")
    public R update(@RequestBody SuperviseQuestionModifyEntity superviseQuestionModify){
        ValidatorUtils.validateEntity(superviseQuestionModify);
        superviseQuestionModifyService.updateAllColumnById(superviseQuestionModify);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:supervisequestionmodify:delete")
    public R delete(@RequestBody Long[] ids){
        superviseQuestionModifyService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    
    /**
     * 反馈审核
     */
    @RequestMapping("/updateById")
    public R updateById(@RequestParam Map<String, String> params ){
    	SuperviseQuestionModifyEntity superviseQuestionModify = new SuperviseQuestionModifyEntity();
    	String shsj = params.get("shsj");
    	String checkStatus = "1";//params.get("checkStatus");//审核状态(0：待审核，1：审核通过，2：审核未通过)
    	if (StringUtils.isNotEmpty(checkStatus)) {
    		superviseQuestionModify.setCheckStatus(checkStatus);
    	}
    	String checkResult = params.get("checkResult");//审核结果(0：考核扣分，1：不予处理，2：其他)
    	if (StringUtils.isNotEmpty(checkResult)) {
    		superviseQuestionModify.setCheckResult(checkResult);
    	}
    	String kffz = params.get("kffz");
    	if (StringUtils.isNotEmpty(kffz)) {
    		superviseQuestionModify.setKffz(Integer.valueOf(kffz));
    	}
    	String checkResultDetail = params.get("checkResultDetail");
    	if (StringUtils.isNotEmpty(checkResultDetail)) {
    		superviseQuestionModify.setCheckResultDetail(checkResultDetail);
    	}
    	
    	SysUserEntity user = getUser();
    	superviseQuestionModify.setFkshrdw(user.getDeptName());
    	superviseQuestionModify.setFkshr(user.getName());
    	superviseQuestionModify.setFkshsj(DateUtils.stringToDate(shsj, "yyyy-MM-dd HH:mm:ss"));
    	
    	superviseQuestionModify.setUpdateUser(user.getUserId());
    	superviseQuestionModify.setUpdateTime(new Date());
    	EntityWrapper<SuperviseQuestionModifyEntity> wrapper = new EntityWrapper<SuperviseQuestionModifyEntity>();
		wrapper.setEntity(new SuperviseQuestionModifyEntity());
		//问题整改ID
		String modifyid = params.get("id");
    	if (StringUtils.isNotEmpty(modifyid)) {
    		superviseQuestionModify.setId(Long.valueOf(modifyid));
    		wrapper.eq("ID", modifyid);
    	}
        superviseQuestionModifyService.update(superviseQuestionModify, wrapper);
        
        return R.ok();
    }
    
    /**
     * 整改反馈(插入整改数据)
     */
    @RequestMapping("/insertQuestionModify")
    public R insertQuestionModify(@RequestParam Map<String, String> params ){
    	Date now = new Date();
    	//插入问题整改数据
    	SuperviseQuestionModifyEntity superviseQuestionModify = new SuperviseQuestionModifyEntity();
    	String dcRecordId = params.get("dcRecordId");//审核结果(0：考核扣分，1：不予处理，2：其他)
    	if (StringUtils.isNotEmpty(dcRecordId)) {
    		superviseQuestionModify.setDcRecordId(Long.valueOf(dcRecordId));
    	}
    	
    	String zgfknr = params.get("zgfknr");
    	if (StringUtils.isNotEmpty(zgfknr)) {
    		superviseQuestionModify.setZgfknr(zgfknr);
    	}
    	
    	SysUserEntity user = getUser();
    	superviseQuestionModify.setFkdw(user.getDeptName());//反馈单位
    	superviseQuestionModify.setFksj(now);//反馈时间
    	superviseQuestionModify.setFkry(user.getName());//反馈人员
    	//superviseQuestionModify.setToDcry(user.getName());//被督察人员
    	superviseQuestionModify.setModifyTime(now);//整改时间
    	superviseQuestionModify.setCreateUser(user.getUserId());
    	superviseQuestionModify.setCreateTime(now);
    	superviseQuestionModify.setUpdateUser(user.getUserId());
    	superviseQuestionModify.setUpdateTime(now);
    	
		String dcQuestionId = params.get("dcQuestionId");
    	if (StringUtils.isNotEmpty(dcQuestionId)) {
    		superviseQuestionModify.setDcQuestionId(Long.valueOf(dcQuestionId));
    	}
        superviseQuestionModifyService.insert(superviseQuestionModify);
        //更新问题表整改状态
        Long questionModifyId = superviseQuestionModify.getId();
        SuperviseQuestionEntity superviseQuestion = new SuperviseQuestionEntity();
        superviseQuestion.setUpdateUser(user.getUserId());
        superviseQuestion.setUpdateTime(new Date());
        superviseQuestion.setModifyStatus("2");//整改状态(0：待整改，1：无整改，2：已整改，3：备存无整改，4：备存待整改)
        EntityWrapper<SuperviseQuestionEntity> wrapper = new EntityWrapper<SuperviseQuestionEntity>();
		wrapper.setEntity(new SuperviseQuestionEntity());
		wrapper.eq("ID", dcQuestionId);
        superviseQuestionService.update(superviseQuestion, wrapper);
        
        int countNum = superviseQuestionService.findSuperviseQuestionForModifyStatusNum(params);
        Long srId = Long.valueOf(params.get("dcRecordId"));
        SuperviseRecordEntity srEntity = new SuperviseRecordEntity();
        if (countNum == 0) {
        	srEntity.setId(srId);
        	srEntity.setModifyStatus("2");
        	superviseRecordService.updateById(srEntity);
        }
        //插入附件
		SupervisionQuestionBean siList = JsonUtil.jsonToBean(params.get("fjPath"), SupervisionQuestionBean.class);
//        String fjPath = params.get("fjPath").replace("[", "").replace("]", "");
		List<SuperviseImageEntity> imgList = siList.getSuperviseImageEntityList();
		List<SuperviseImageEntity> addImgList = new ArrayList<SuperviseImageEntity>();
        if (imgList != null && imgList.size() != 0) {
        	for (SuperviseImageEntity siEntity : imgList) {
	        		//insert SUPERVISE_IMAGE
	        		File tempFile =new File(siEntity.getPicUrl().trim());
	                String fileName = tempFile.getName();
	        		siEntity.setWtglId(questionModifyId);
	        		siEntity.setPicName(fileName);
	        		siEntity.setPicType("2");//类型(1：督察问题，2：整改问题，3：督察备存)
	        		siEntity.setQf("0");
	        		siEntity.setCreateUser(user.getUserId());
	        		siEntity.setCreateTime(now);
	        		siEntity.setUpdateUser(user.getUserId());
	        		siEntity.setUpdateTime(now);
					addImgList.add(siEntity);
	        	}
			superviseImageService.insertBatch(addImgList);
        }

        return R.ok().put("id", superviseQuestionModify.getId());
    }


}
